package com.monitoring.domain.valueobject;

import lombok.Value;

@Value
public class Temperature {
    private double value;
    private String unit; // Celsius, Fahrenheit
    
    public Temperature(double value, String unit) {
        if (value < -100 || value > 100) {
            throw new IllegalArgumentException("Temperature value out of valid range");
        }
        if (!"Celsius".equals(unit) && !"Fahrenheit".equals(unit)) {
            throw new IllegalArgumentException("Invalid temperature unit");
        }
        this.value = value;
        this.unit = unit;
    }
    
    public Temperature convertTo(String targetUnit) {
        if (this.unit.equals(targetUnit)) {
            return this;
        }
        
        double convertedValue;
        if (targetUnit.equals("Celsius")) {
            convertedValue = (value - 32) * 5 / 9; // Fahrenheit to Celsius
        } else {
            convertedValue = (value * 9 / 5) + 32; // Celsius to Fahrenheit
        }
        
        return new Temperature(Math.round(convertedValue * 100) / 100.0, targetUnit);
    }
    
    public boolean isHigh() {
        double celsiusValue = this.unit.equals("Celsius") ? value : convertTo("Celsius").value;
        return celsiusValue > 35;
    }
    
    public boolean isLow() {
        double celsiusValue = this.unit.equals("Celsius") ? value : convertTo("Celsius").value;
        return celsiusValue < 0;
    }
}